<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>while do..while验证用户名和密码</title>
    <script>
        function loginWithWhile() {
            var username = "wkf";
            var password = "123456";
            var inputUsername = "";
            var inputPassword = "";

            while (inputUsername !== username || inputPassword !== password) {
                inputUsername = prompt("请输入用户名:");
                inputPassword = prompt("请输入密码:");

                if (inputUsername !== username || inputPassword !== password) {
                    alert("用户名或密码错误，请重新输入！");
                }
            }

            alert("登录成功！欢迎" + inputUsername + "！");
        }

        function loginWithDoWhile() {
            var username = "wkf";
            var password = "123456";
            var inputUsername;
            var inputPassword;

            do {
                inputUsername = prompt("请输入用户名:");
                inputPassword = prompt("请输入密码:");

                if (inputUsername !== username || inputPassword !== password) {
                    alert("用户名或密码错误！");
                }
            } while (inputUsername !== username || inputPassword !== password);

            alert("登录成功！欢迎" + inputUsername + "！");
        }
    </script>
</head>
<body>
    <button onclick="loginWithWhile()">使用 while 登录</button>
    <button onclick="loginWithDoWhile()">使用 do...while 登录</button>
</body>
</html>